//
//  MarsEmergencyContactManager.m
//  MarsWebDemo
//
//  Created by yangpenghua on 2018/4/2.
//  Copyright © 2018年 linzi. All rights reserved.
//

#import "MarsEmergencyContactManager.h"

#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>

#import "UIViewController+MarsVisible.h"

@interface MarsEmergencyContactManager ()<ABPeoplePickerNavigationControllerDelegate>

@property (nonatomic, strong) ABPeoplePickerNavigationController *addressPicker;
@end

@implementation MarsEmergencyContactManager

#pragma mark - 通讯录
#pragma mark - 系统通讯录
- (void)mars_systemAddressBookViewController
{
    if(!self.addressPicker){
        self.addressPicker = [[ABPeoplePickerNavigationController alloc] init];
        // place the delegate of the picker to the controll
        self.addressPicker.peoplePickerDelegate = self;
    }
    // showing the picker
    UIViewController *currentVC = [UIViewController mars_currentViewController];
    [currentVC presentViewController:self.addressPicker animated:YES completion:nil];
}

/*
 该方法在用户选择通讯录一级列表的某一项时被调用,通过person可以获得选中联系人的所有信息,但当选中的联系人有多个号码,而我们又希望用户可以明确的指定一个号码时(如拨打电话),返回YES允许通讯录进入联系人详情界面:
 */
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person{
    return YES;
}

/*
 当用户点击某个字段时,会调用如下方法:
 联系人信息中可能有很多字段,首先需要判断选择的是否为电话号码字段.当满足要求时,获取联系人信息,通过标识符获得用户选择的号码在该联系人号码列表中的索引,最后通过索引获得选中的电话号码. IOS(2_0, 8_0)
 */
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier{
    if (property == kABPersonPhoneProperty) {
        //名字
        NSString *name = (__bridge_transfer NSString *) ABRecordCopyCompositeName(person);
        NSLog(@"name:%@",name);
        if (!name) {
            name = @"";
        }
        //电话
        ABMutableMultiValueRef phoneMulti = ABRecordCopyValue(person, property);
        NSInteger index = ABMultiValueGetIndexForIdentifier(phoneMulti,identifier);
        
        NSString *phone = (__bridge NSString*)ABMultiValueCopyValueAtIndex(phoneMulti, index);
        phone = [self trimSting:phone];
        
        __weak typeof(self) weakSelf = self;
        //do something
        [self.addressPicker dismissViewControllerAnimated:YES completion:^{
            if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(emergencyContactSelectPersonName:personPhone:)]) {
                [weakSelf.delegate emergencyContactSelectPersonName:name personPhone:phone];
            }
        }];
    }
    
    return NO;
}

//IOS 8 调用 下面两个函数
//- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person NS_AVAILABLE_IOS(8_0)
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
{
    
    //do something
    if (property == kABPersonPhoneProperty) {
        //名字
        NSString *name = (__bridge_transfer NSString *) ABRecordCopyCompositeName(person);
        NSLog(@"name:%@",name);
        if (!name) {
            name = @" ";
        }
        //电话
        ABMutableMultiValueRef phoneMulti = ABRecordCopyValue(person, property);
        NSInteger index = ABMultiValueGetIndexForIdentifier(phoneMulti,identifier);
        
        NSString *phone = (__bridge NSString*)ABMultiValueCopyValueAtIndex(phoneMulti, index);
        phone = [self trimSting:phone];
        
        __weak typeof(self) weakSelf = self;
        //do something
        [self.addressPicker dismissViewControllerAnimated:YES completion:^{
            if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(emergencyContactSelectPersonName:personPhone:)]) {
                [weakSelf.delegate emergencyContactSelectPersonName:name personPhone:phone];
            }
        }];
    }
}

/*
 最后还需要实现如下方法使得用户在点击"取消"按钮时关闭联系人选择界面:
 */
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker
{
    [self.addressPicker dismissViewControllerAnimated:YES completion:^{
    }];
}

#pragma mark 去掉不要的字符（比如：-、（）等等）
- (NSString *)trimSting:(NSString *)str
{
    str = [str stringByReplacingOccurrencesOfString:@"(" withString:@""];
    str = [str stringByReplacingOccurrencesOfString:@")" withString:@""];
    str = [str stringByReplacingOccurrencesOfString:@"-" withString:@""];
    str = [str stringByReplacingOccurrencesOfString:@" " withString:@""];
    str = [str stringByReplacingOccurrencesOfString:@" " withString:@""];
    
    return str;
}

@end
